package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.LinkedProblem;
import cn.pugle.oj.catalog.Unknown;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 牛客网的
 *
 * @author tzp
 * @since 2020/12/18
 */
public class LC146_1 implements LinkedProblem {
    public static class LRUMap extends LinkedHashMap<Integer, Integer> {
        int max;

        public LRUMap(int max) {
            super(max, 0.75f, true);
            this.max = max;
        }

        @Override
        public boolean removeEldestEntry(Map.Entry eldest) {
            return this.size() > max;
        }
    }

    public int[] LRU(int[][] operators, int k) {
        // write code here
        LRUMap lru = new LRUMap(k);
        int[] rt = new int[operators.length];
        int x = 0;
        for (int i = 0; i < operators.length; i++) {
            if (operators[i][0] == 1) {
                lru.put(operators[i][1], operators[i][2]);
            } else {
                Integer integer = lru.get(operators[i][1]);
                rt[x] = integer == null ? -1 : integer;
                x++;
            }
        }

        return Arrays.copyOfRange(rt, 0, x);
    }

    public static void main(String[] args) {
        int[][] x = new int[][]{{1, 1, 1}, {1, 2, 2}, {1, 3, 2}, {2, 1}, {1, 4, 4}, {2, 2}};
        System.out.println(Arrays.toString(new LC146_1().LRU(x, 3)));
    }
}
